package com.dididoctor.doctor.Activity.Main.Inquiry.InquiryDetail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dididoctor.doctor.MV.adapter.CommonAdapter;
import com.dididoctor.doctor.MV.adapter.ViewHolder;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.ItemListView;
import com.dididoctor.doctor.Utils.Util;

import java.util.ArrayList;
import java.util.List;


/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： InquiryDetailAdapter
 * @包名称： com.dididoctor.doctor.Activity.Main.Inquiry.InquiryDetail
 * @创建人： zhanjunjie
 * @创建时间：2016/4/20 13:53
 * @修改人：
 * @修改时间：2016/4/20 13:53
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class InquiryDetailAdapter extends BaseAdapter {

    private static final int TYPE_COUNT = 10;//item类型的总数
    private static final int TYPE_ANSWER = 1;//问答类型
    private static final int TYPE_SELECT = 2;//单选和多选类型
    private int currentType;//当前item类型
    private Context context;
    private DeleteClick deleteClick;

    //数据
    private List<InquiryDetail> inquiryDetails = new ArrayList<>();
    private String diseId = "";

    public InquiryDetailAdapter(Context context, List<InquiryDetail> inquiryDetails, String diseId, DeleteClick deleteClick) {
        this.context = context;
        this.inquiryDetails = inquiryDetails;
        this.diseId = diseId;
        this.deleteClick = deleteClick;
    }

    @Override
    public int getItemViewType(int position) {
        if ("1".equals(inquiryDetails.get(position).getType())) {
            return TYPE_ANSWER;// 问答类型
        } else if ("2".equals(inquiryDetails.get(position).getType()) || "3".equals(inquiryDetails.get(position).getType())) {
            return TYPE_SELECT;// 选则类型
        } else {
            return 100;
        }
    }


    public void addData(List<InquiryDetail> inquiries) {
        if (this.inquiryDetails == null) {
            this.inquiryDetails = new ArrayList<InquiryDetail>();
        }
        this.inquiryDetails.addAll(inquiries);
        notifyDataSetChanged();
    }

    public void setData(List<InquiryDetail> datas) {
        this.inquiryDetails = datas;
        notifyDataSetChanged();
    }


    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getCount() {
        return inquiryDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return inquiryDetails.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //视图数据初始化
        View answerView = null;
        View selectView = null;
        SelectAdapter adapter = null;
        //获得视图类型
        currentType = getItemViewType(position);
        if (currentType == TYPE_ANSWER) { //如果是问答
            final InquiryDetail inquiryDetail = this.inquiryDetails.get(position);
            AnswerHolder holder = null;
            if (holder == null) {
                holder = new AnswerHolder();
                answerView = LayoutInflater.from(context).inflate(R.layout.item_inquiry_one, null);
                holder.mImgDelete = (ImageView) answerView.findViewById(R.id.img_delete);
                holder.mTVAnswerTitle = (TextView) answerView.findViewById(R.id.tv_answertitle);
                holder.mView = answerView.findViewById(R.id.view);
                answerView.setTag(holder);
                convertView = answerView;
            } else {
                holder = (AnswerHolder) answerView.getTag();
            }

            //控件处理
            holder.mTVAnswerTitle.setText("(" + Util.toString(position + 1) + ")" + inquiryDetail.getContent());
            if ("1".equals(inquiryDetail.getIsSys())) {
                holder.mImgDelete.setVisibility(View.GONE);
            } else {
                holder.mImgDelete.setVisibility(View.VISIBLE);
            }
            holder.mImgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteClick.deleteinquiry(inquiryDetail.getQusId());
                }
            });

            //是否显示底部的条
            if (inquiryDetail.isshow()) {
                holder.mView.setVisibility(View.VISIBLE);
            } else {
                holder.mView.setVisibility(View.GONE);
            }

        } else if (currentType == TYPE_SELECT) {
            final InquiryDetail inquiryDetail = inquiryDetails.get(position);
            SelectHolder holder = null;
            if (holder == null) {
                holder = new SelectHolder();
                selectView = LayoutInflater.from(context).inflate(R.layout.item_inquiry_two, null);
                holder.mImgDelete = (ImageView) selectView.findViewById(R.id.img_delete);
                holder.mTvSelectTitle = (TextView) selectView.findViewById(R.id.tv_answertitle);
                holder.mLvSelect = (ItemListView) selectView.findViewById(R.id.listview);
                holder.mLinAll = (LinearLayout) selectView.findViewById(R.id.lin_all);
                holder.mView = selectView.findViewById(R.id.view);
                selectView.setTag(holder);
                convertView = selectView;
            } else {
                holder = (SelectHolder) selectView.getTag();
            }

            //控件处理
            if ("3".equals(inquiryDetail.getType())) {
                holder.mTvSelectTitle.setText("(" + Util.toString(position + 1) + ")" + inquiryDetail.getContent() + "(可多选)");
            } else if ("2".equals(inquiryDetail.getType())) {
                holder.mTvSelectTitle.setText("(" + Util.toString(position + 1) + ")" + inquiryDetail.getContent() + "(单选)");
            }


            if ("1".equals(inquiryDetail.getIsSys())) {
                holder.mImgDelete.setVisibility(View.GONE);
            } else {
                holder.mImgDelete.setVisibility(View.VISIBLE);
            }
            holder.mImgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteClick.deleteinquiry(inquiryDetail.getQusId());
                }
            });
            if (inquiryDetail.isshow()) {
                holder.mView.setVisibility(View.VISIBLE);
            } else {
                holder.mView.setVisibility(View.GONE);
            }
            //listview处理
            List<String> strings = new ArrayList<>();
            String[] opction = inquiryDetail.getOptions().split(",\\|,");
            for (int i = 0; i < opction.length; i++) {
                strings.add(opction[i]);
            }

            adapter = new SelectAdapter(context, strings);
            holder.mLvSelect.setAdapter(adapter);

        }
        return convertView;
    }

    class SelectAdapter extends CommonAdapter<String> {

        private TextView mTvDiasease;

        public SelectAdapter(Context context, List<String> datas) {
            super(context, datas, R.layout.item_item_inquiry);
        }

        @Override
        public void convert(int position, ViewHolder helper, String item) {
            mTvDiasease = helper.getView(R.id.tv_diasease);
            mTvDiasease.setText(item);
        }

    }

    //简答题的hold
    class AnswerHolder {
        TextView mTVAnswerTitle;
        ImageView mImgDelete;
        View mView;
    }

    //多选和单选hold
    class SelectHolder {
        TextView mTvSelectTitle;
        ImageView mImgDelete;
        ItemListView mLvSelect;
        View mView;
        LinearLayout mLinAll;
    }

    //调用接口回调
    public interface DeleteClick {
        void deleteinquiry(String qusId);
    }
}
